<template>
	<xn-form-container
		:title="formData.id ? '编辑客户' : '增加客户'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="用户名称：" name="username">
				<a-input v-model:value="formData.username" placeholder="请输入用户名称" allow-clear />
			</a-form-item>
			<a-form-item label="手机号：" name="phone">
				<a-input v-model:value="formData.phone" placeholder="请输入手机号" allow-clear />
			</a-form-item>
			<a-form-item label="公司名称：" name="company_name">
				<a-input v-model:value="formData.company_name" placeholder="请输入公司名称" allow-clear />
			</a-form-item>
			<a-form-item label="邮件：" name="email">
				<a-input v-model:value="formData.email" placeholder="请输入邮件" allow-clear />
			</a-form-item>
			<a-form-item label="密码：" name="password">
				<a-input v-model:value="formData.password" placeholder="请输入密码" allow-clear />
			</a-form-item>
			<a-form-item label="微信：" name="open_id">
				<a-input v-model:value="formData.open_id" placeholder="请输入微信" allow-clear />
			</a-form-item>
			<a-form-item label="状态：" name="state">
				<a-select
					v-model:value="formData.state"
					placeholder="请选择状态"
					:options="[
						{ label: '正常', value: 1 },
						{ label: '注销禁用', value: 0 }
					]"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="性别：" name="gender">
				<a-select
					v-model:value="formData.gender"
					placeholder="请选择性别"
					:options="[
						{ label: '男', value: true },
						{ label: '女', value: false }
					]"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		index = id
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	// 默认要校验的
	const formRules = {
		username: [required('请输入客户名称')],
		phone: { required: true, ...rules.phone },
		company_name: [required('请输入公司名称')],
		email: { required: true, ...rules.email },
		open_id: [required('请输入微信')],
		state: [required('请选择状态')],
		gender: [required('请选择性别')],
		password: [required('请输入密码')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const json = {}
				api
					.submitForm(
						{
							...formData.value,
							id: formData.value.id || index,
							...(formData.value.id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch(() => {})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
